<!--
  删除提示组件       zls-2021/2/11
      Attributes
          1、title          弹框标题名称
          2、text           提示内容
          3、btns           操作按钮数组
          4、canHandle      是否可点击显示弹框
          其他所有属性请参照 Popup.vue 公共组件

      Demo==>
          <z-delToast @sure="deleteFn">
            <div class="delete">删除</div>
          </z-delToast>
-->
<template>
  <div class="z-delToast">
    <z-popup :position="'center'" :show="show" :width="$attrs.width || 400" :height="$attrs.height || 200" :round="$attrs.round" :zIndex="$attrs.zIndex || 9000" @close="close">
      <div class="z-delToast-main">
        <div class="z-modal-main-title">
          <span>{{ title }}</span>
          <i class="close" @click.stop="close"></i>
        </div>
        <div class="z-delToast-main-content scrollbar">{{ text }}</div>
        <ul class="z-delToast-main-btn" v-if="btns.length">
          <li v-for="(btnItem, btnI) in btns" :key="btnI" class="btnRipple" @click.stop="btnClick(btnItem.event)" :style="{ backgroundColor: btnItem.bgColor }">
            {{ btnItem.text }}
          </li>
        </ul>
      </div>
    </z-popup>
    <div @click.stop="handleClick" style="cursor: pointer; display: flex">
      <slot></slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
export default defineComponent({
  name: 'z-delToast',
  inheritAttrs: false,
  props: {
    title: {
      type: String,
      default: '提示'
    },
    text: {
      type: String,
      default: '确定删除该项吗?'
    },
    btns: {
      type: Array as () => Array<any>,
      default: () => [
        {
          text: '取消',
          type: 1,
          event: 'cancel'
        },
        {
          text: '确定',
          type: 2,
          event: 'sure'
        }
      ]
    },
    canHandle: {
      type: Boolean,
      default: true
    }
  },
  setup(props, { emit }) {
    const show = ref<boolean>(false)
    init()

    function init() {
      if (props.btns.length) {
        props.btns.forEach(v => {
          v.bgColor = v.type === 1 ? '#a6a9ad' : v.type === 2 ? 'var(--el-color-primary)' : ''
        })
      }
    }

    function handleClick() {
      show.value = props.canHandle
    }

    function close() {
      show.value = false
      emit('close')
    }

    function btnClick(val: string) {
      show.value = false
      emit(val)
    }

    return { show, handleClick, close, btnClick }
  }
})
</script>

<style scoped lang="scss">
.z-delToast {
  &-main {
    width: 100%;
    height: 100%;
    .z-modal-main-title {
      height: 50px;
      text-align: center;
      line-height: 50px;
      position: relative;
      cursor: grabbing;
      color: #fff;
      background-image: linear-gradient(90deg, var(--el-color-primary), var(--el-color-primary-light-3), var(--el-color-primary));
      &:after {
        content: '';
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        height: 1px;
        background-image: linear-gradient(90deg, #053556, #05849c, #053556);
      }
      > span {
        font-size: 20px;
        font-weight: 900;
        letter-spacing: 6px;
        user-select: none;
      }
      .close {
        position: absolute;
        right: 15px;
        top: 13px;
        width: 20px;
        height: 20px;
        cursor: pointer;
        &:before,
        &:after {
          position: absolute;
          content: ' ';
          height: 20px;
          width: 2px;
          border-radius: 2px;
          background-color: #fff;
        }
        &:before {
          transform: rotate(45deg);
        }
        &:after {
          transform: rotate(-45deg);
        }
      }
    }
    &-content {
      width: 100%;
      height: calc(100% - 110px);
      font-size: 18px;
      line-height: 24px;
      text-align: center;
      letter-spacing: 2px;
      overflow-y: auto;
      padding: 30px 15px 0 15px;
    }
    &-btn {
      height: 60px;
      @include flex(center, center);
      > li {
        width: 100px;
        height: 32px;
        margin: 0 10px;
        font-size: 16px;
        font-weight: 900;
        color: #fff;
        letter-spacing: 4px;
        border-radius: 3px;
        cursor: pointer;
        user-select: none;
        @include flex(center, center);
      }
    }
  }
}
</style>
